package model.readers.PMetro;

import com.nightpiter.model.objects.Coordinates;


import model.MetroModel;
import model.objects.EdiStation;

public class ShiftModel {

	public ShiftModel(MetroModel model) {

		Coordinates point;

		int dX = toCenter(model).x;
		int dY = toCenter(model).y;
		for (EdiStation station : model) {
			point = station.getPosition();
			point.x = (int) point.x - dX;
			point.y = (int) point.y - dY;
			station.setPosition(point);

		}

	}

	Coordinates toCenter(MetroModel model) {

		Coordinates point;

		int maxX = 0, maxY = 0, minX = 0, minY = 0;
		boolean first = true;
		for (EdiStation station : model) {
			if (first) {
				point = station.getPosition();
				maxX = minX = point.x;
				maxY = minY = point.x;
				first = false;
			} else {
				point = station.getPosition();

				if (point.x < minX)
					minX = point.x;
				if (point.x > maxX)
					maxX = point.x;
				if (point.y < minY)
					minY = point.y;
				if (point.y > maxY)
					maxY = point.y;
			}
		}

		Coordinates returnPoint = new Coordinates((int) ((maxX + minX) / 2),
				(int) ((maxY + minY) / 2));

		return returnPoint;
	}

}
